<?php 
defined('RUN_KATANA') or die('Restricted access');
class SiteAdmin extends Object
{
	public $SiteId;
	public $AjaxRun;
	public $Site;
	public $Sitemap;
	public $SitemapItem;
	
	public function SiteAdmin()
	{
		global $commander;
		self::setDataTablePrefix('sitemap_');
		self::setDataTable('pages');
		self::setSiteId(SITE_ID);
		if(defined('AJAX_RUN'))
		{ self::setAjaxRun(true); }
		else
		{ self::setAjaxRun(false); }
		self::runPreActions();
		$this->Site = new Site($this->SiteId);
		$this->loadSitemap();
		self::runPostActions();
	}
	
	/* Get Functions */ 
	public function getSiteId() { return $this->SiteId; }
	public function getAjaxRun() { return $this->AjaxRun; }
	
	/* Set Functions */ 
	public function setSiteId($val) { $this->SiteId=$val; }
	public function setSitemap($val) { $this->Sitemap = $val; }
	public function setAjaxRun($val) { $this->AjaxRun=$val; }
	
	public function runPreActions()
	{
		switch($_POST['action'])
		{
			case 'editor_sitemap':
				if(($_POST['new_m_item'] != '') && ($_REQUEST['m'] > 0) && (is_numeric($_POST['new_m_item_type'])))
				{ self::addMenuItem($_REQUEST['m'], $_POST['new_m_item'], $_POST['new_m_item_type']); }
				return;
			break;
		}
	}
	
	public function runPostActions()
	{
		global $commander;
		switch ($_POST['action'])
		{
			case 'editor_site':
				if($this->Site)
				{
					$this->Site->setTitle(stripslashes($commander->stripNonAlphaNum($_POST['title'])));
					$this->Site->setURL(stripslashes($_POST['url']));
					$this->Site->setLicense(stripslashes($commander->stripNonAlphaNum($_POST['license'])));
					$this->Site->setLanguage(stripslashes($_POST['language']));
					$this->Site->setMetaTitle($commander->formatText($_POST['MetaTitle']));
					$this->Site->setMetaData($commander->formatText($_POST['meta_data']));
					$this->Site->setMetaKey($commander->formatText($_POST['meta_key']));
					$this->Site->setAnalyticsID($commander->formatText($_POST['analytics_id']));
					$this->Site->setCompanyName($commander->formatText($_POST['company_name']));
					$this->Site->setCompanyNumber($commander->stripNonAlphaNum($_POST['company_number']));
					$this->Site->setCompanyAddress($commander->formatText($_POST['company_address']));
					$this->Site->setCompanyPhone(stripslashes($_POST['company_phone']));
					if($commander->isEmail($_POST['company_email']))
					{ $this->Site->setCompanyEmail(stripslashes($_POST['company_email'])); }
					else
					{ $commander->addError('company_email', ucfirst($txt->getWord('invalid').' '.$txt->getWord('email'))); }
					$this->Site->update();
				}
			break;
			case 'editor_sitemap':
				if(isset($this->Sitemap->CurrentPage))
				{
					/* Item field actions */
					$this->Sitemap->CurrentPage->setSearchIndex('');
					if(stripslashes($_POST['title']) != $this->Sitemap->CurrentPage->getTitle())
					{ 
						$this->Sitemap->CurrentPage->setTitle($commander->formatText($_POST['title'])); 
						$this->Sitemap->CurrentPage->setAlias($commander->formatAlias($this->Sitemap->CurrentPage->getTitle()));
						$this->Sitemap->CurrentPage->addToSearchIndex($this->Sitemap->CurrentPage->getTitle());
						$this->Sitemap->CurrentPage->setDataChanged(true);
					}
					if(stripslashes($_POST['Description']) != $this->Sitemap->CurrentPage->getDescription())
					{ 
						$this->Sitemap->CurrentPage->setDescription($commander->formatHTML($_POST['Description'])); 
						$this->Sitemap->CurrentPage->addToSearchIndex($this->Sitemap->CurrentPage->getDescription());
						$this->Sitemap->CurrentPage->setDataChanged(true);
					}
					if(stripslashes($_POST['MetaTitle']) != $this->Sitemap->CurrentPage->getMetaTitle())
					{ 
						$this->Sitemap->CurrentPage->setMetaTitle($commander->formatText($_POST['MetaTitle'])); 
						$this->Sitemap->CurrentPage->addToSearchIndex($this->Sitemap->CurrentPage->getMetaTitle());
						$this->Sitemap->CurrentPage->setDataChanged(true);
					}
					if(stripslashes($_POST['meta_desc']) != $this->Sitemap->CurrentPage->getMetaDesc())
					{ 
						$this->Sitemap->CurrentPage->setMetaDesc($commander->formatText($_POST['meta_desc'])); 
						$this->Sitemap->CurrentPage->addToSearchIndex($this->Sitemap->CurrentPage->getMetaDesc());
						$this->Sitemap->CurrentPage->setDataChanged(true);
					}
					if(stripslashes($_POST['meta_key']) != $this->Sitemap->CurrentPage->getMetaKey())
					{ 
						$this->Sitemap->CurrentPage->setMetaKey($commander->formatText($_POST['meta_key'])); 
						$this->Sitemap->CurrentPage->addToSearchIndex($this->Sitemap->CurrentPage->getMetaKey());
						$this->Sitemap->CurrentPage->setDataChanged(true);
					}
					
					if(stripslashes($_POST['static_html']) != $this->Sitemap->CurrentPage->getStaticHtml())
					{ 
						$this->Sitemap->CurrentPage->setStaticHtml($commander->formatHTML($_POST['static_html'])); 
						//$this->Sitemap->CurrentPage->addToSearchIndex($commander->formatText($this->Sitemap->CurrentPage->getStaticHtml()));
						$this->Sitemap->CurrentPage->setDataChanged(true);
					}
					if(intval($_POST['FormId']) != $this->Sitemap->CurrentPage->getFormId())
					{ 
						$this->Sitemap->CurrentPage->setFormId(intval($_POST['FormId'])); 
						$this->Sitemap->CurrentPage->setDataChanged(true);
						echo 'setFormId'.intval($_POST['FormId']);
					}
					
					if($_FILES['Image'])
					{
						$file = $_FILES['Image'];
						if($file["name"] != '')
						{
							if($file['size'] < 1000000)
							{ $this->Sitemap->CurrentPage->uploadImage($_FILES['Image']); }
							else
							{ $commander->ErrorField['Image'] = 'image file size exceded 1mb'; }
						}
						$this->Sitemap->CurrentPage->setDataChanged(true);
					}
					if(intval($_POST['ImageDelete']) > 0)
					{ 
						$this->Sitemap->CurrentPage->deleteImage(); 
						$this->Sitemap->CurrentPage->setDataChanged(true);
					}
					
					if($this->Sitemap->CurrentPage->getDataChanged())
					{ 
						$this->Sitemap->CurrentPage->setSearchIndex($commander->formatSearchIndex($this->Sitemap->CurrentPage->getSearchIndex()));
						$this->Sitemap->CurrentPage->update(); 
					}
					/* Item field actions end */
					
					if(isset($this->Sitemap->CurrentPage->SubItems))
					{
						/* Subitems list actions */
						if(count($_POST['m_id']) > 0)
						{
							$i=0;
							foreach($_POST['m_id'] as $id)
							{
								if($_POST['m_title'][$i] != $this->Sitemap->CurrentPage->SubItems[$id]->getTitle())
								{ 
									$this->Sitemap->CurrentPage->SubItems[$id]->setTitle($commander->formatText($_POST['m_title'][$i]));
									$this->Sitemap->CurrentPage->SubItems[$id]->setAlias($commander->formatAlias($_POST['m_title'][$i]));
									$this->Sitemap->CurrentPage->SubItems[$id]->setDataChanged(true);
								}
								if($_POST['m_item_link_type'][$i] != $this->Sitemap->CurrentPage->SubItems[$id]->getLinkType())
								{ 
									$this->Sitemap->CurrentPage->SubItems[$id]->setLinkType($_POST['m_item_link_type'][$i]);
									$this->Sitemap->CurrentPage->SubItems[$id]->setDataChanged(true);
								}
								if($_POST['m_link_target'][$i] != $this->Sitemap->CurrentPage->SubItems[$id]->getLinkTarget())
								{ 
									$this->Sitemap->CurrentPage->SubItems[$id]->setLinkTarget($_POST['m_link_target'][$i]);
									$this->Sitemap->CurrentPage->SubItems[$id]->setDataChanged(true);
								}
								if($_POST['m_link_url'][$i] != $this->Sitemap->CurrentPage->SubItems[$id]->getLinkUrl())
								{ 
									$this->Sitemap->CurrentPage->SubItems[$id]->setLinkUrl($_POST['m_link_url'][$i]);
									$this->Sitemap->CurrentPage->SubItems[$id]->setDataChanged(true);
								}
								if($_POST['m_link_id'][$i] != $this->Sitemap->CurrentPage->SubItems[$id]->getLinkId())
								{ 
									$this->Sitemap->CurrentPage->SubItems[$id]->setLinkId($_POST['m_link_id'][$i]);
									$this->Sitemap->CurrentPage->SubItems[$id]->setDataChanged(true);
								}
								if($_POST['m_access'][$i] != $this->Sitemap->CurrentPage->SubItems[$id]->getAccess())
								{ 
									$this->Sitemap->CurrentPage->SubItems[$id]->setAccess($_POST['m_access'][$i]);
									$this->Sitemap->CurrentPage->SubItems[$id]->setDataChanged(true);
								}
								/*
								if($_POST['m_order'][$i] != $this->Sitemap->CurrentPage->SubItems[$id]->MenuOrder)
								{ 
									$commander->setTableItemOrder($id, $this->Sitemap->CurrentPage->SubItems[$id]->MenuOrder, $_POST['m_order'][$i], self::getDataTablePrefix().self::getDataTable(), 'menu_order'); 
									$this->Sitemap->CurrentPage->setSubLoaded(false);
								}
								*/
								$this->Sitemap->CurrentPage->SubItems[$id]->update();
								$i++;
							}
							if(($_POST['activate'] > 0) && (isset($this->Sitemap->CurrentPage->SubItems[$_POST['activate']])))
							{ 
								$this->Sitemap->CurrentPage->SubItems[$_POST['activate']]->setActive(1);
								$commander->setTableActive($_POST['activate'], self::getDataTablePrefix().self::getDataTable(), 1); 
							}
							if(($_POST['deactivate'] > 0) && (isset($this->Sitemap->CurrentPage->SubItems[$_POST['deactivate']])))
							{ 
								$this->Sitemap->CurrentPage->SubItems[$_POST['deactivate']]->setActive(0);
								$commander->setTableActive($_POST['deactivate'], self::getDataTablePrefix().self::getDataTable(), 0);  
							}
							if(($_POST['visible'] > 0) && (isset($this->Sitemap->CurrentPage->SubItems[$_POST['visible']])))
							{ 
								$this->Sitemap->CurrentPage->SubItems[$_POST['visible']]->setVisible(1);
								$commander->setTableFieldValueOnId(self::getDataTablePrefix().self::getDataTable(), 'visible', 1, $_POST['visible']); 
							}
							if(($_POST['invisible'] > 0) && (isset($this->Sitemap->CurrentPage->SubItems[$_POST['invisible']])))
							{ 
								$this->Sitemap->CurrentPage->SubItems[$_POST['invisible']]->setVisible(0);
								$commander->setTableFieldValueOnId(self::getDataTablePrefix().self::getDataTable(), 'visible', 0, $_POST['invisible']);
							}
							
							if($_POST['selected'])
							{
								foreach($_POST['selected'] as $id)
								{
									if(isset($this->Sitemap->CurrentPage->SubItems[$id]))
									{
										if($_POST['activate'] == '-1')
										{
											$this->Sitemap->CurrentPage->SubItems[$id]->setActive(1);
											$commander->setTableActive($id, self::getDataTablePrefix().self::getDataTable(), 1); 
										}
										if($_POST['deactivate'] == '-1')
										{
											$this->Sitemap->CurrentPage->SubItems[$id]->setActive(0);
											$commander->setTableActive($id, self::getDataTablePrefix().self::getDataTable(), 0); 
										}
									}
								}
							}	
							
							if($_POST['up'] > 0)
							{ 
								$commander->setTableItemOrderUp($_POST['up'], self::getDataTablePrefix().self::getDataTable(), 'menu_order'); 
								$this->Sitemap->CurrentPage->setSubLoaded(false);
							}
							if($_POST['down'] > 0)
							{ 
								$commander->setTableItemOrderDown($_POST['down'], self::getDataTablePrefix().self::getDataTable(), 'menu_order'); 
								$this->Sitemap->CurrentPage->setSubLoaded(false);
							}
						} 
						/* Subitems list actions End */
					}
				}
				if($this->Sitemap->CurrentPage->getSubLoaded() == false)
				{ $this->Sitemap->CurrentPage->loadSubItems(); }
			break;
		}
	}
	
	function loadSitemap()
	{
		if($this->Site)
		{
			$this->Sitemap = new SitemapController;
			if(!($this->Sitemap->Frontpage))
			{
				global $txt;
				global $commander;
				$sql  = 'INSERT INTO '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' ';
				$sql .= '(`parent_id`, `site_id`, `frontpage`, `subitems`, `title`, `menu_order`) ';
				$sql .= 'VALUES ';
				$sql .= '(0, '.$this->SiteId.', 1, 0, "'.ucfirst($txt->getWord('frontpage')).'", 0);';
				$result = mysql_query($sql);
				$this->Sitemap->loadSitemap();
			}
		}
	}
	
	function addMenuItem($parent, $title, $type)
	{
		if(($title != '') && (is_numeric($type)))
		{
			global $commander;
			$neworder = 0;
			$result = mysql_query('SELECT menu_order FROM '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' WHERE parent_id='.$parent.' ORDER BY menu_order DESC LIMIT 1;');
			if(mysql_num_rows($result) > 0)
			{
				while($row = mysql_fetch_array($result))
				{ 
					$neworder = $row['menu_order'];
					$neworder++;
				}
			}
			
			if(strlen($title) > 100)
			{ $title = substr($title, 0, 30); }
			
			$title = $commander->formatText($title);
			$alias = $commander->formatAlias($title);
			
			$sql = 'INSERT INTO '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' ';
			$sql .= '(`parent_id`, `title`, `alias`, `link_type`, `menu_order`, `site_id`) ';
			$sql .= 'VALUES ';
			$sql .= '('.$parent.', "'.$title.'", "'.$alias.'",'.$type.', '.$neworder.', '.$this->SiteId.');';
			$result = mysql_query($sql);
			if($parent > 0)
			{ $result = mysql_query('UPDATE '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' SET subitems=1 WHERE id='.$parent.';'); }
		}
	}
	
	function getSitemap($site_id)
	{
		if($site_id > 0)
		{
			$result = mysql_query('SELECT * FROM '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' WHERE site_id='.$site_id.' ORDER BY menu_order ASC;');
			$i=0;
			while($row = mysql_fetch_array($result))
			{
				foreach(array_keys($row) as $key)
				{ $item[$i][$key] = $row[$key]; }
				$i++;
			}
			return $item;
		}
	}
	
	function getSitemapTree($site_id, $parent_id=0)
	{
		if($site_id > 0)
		{
			$result = mysql_query('SELECT * FROM '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' WHERE site_id='.$site_id.' AND parent_id='.$parent_id.' ORDER BY menu_order ASC;');
			$i=0;
			while($row = mysql_fetch_array($result))
			{
				foreach(array_keys($row) as $key)
				{ $item[$i][$key] = $row[$key]; }
				$i++;
			}
			return $item;
		}
	}
	
	function getProductSections($site_id, $parent_id=0)
	{
		$i=0;
		if(($parent_id == 0) || ($parent_id > 0))
		{
			$result = mysql_query('SELECT * FROM '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' WHERE parent_id='.$parent_id.' AND site_id='.$site_id.';');
			if($result)
			{
				while($row = mysql_fetch_array($result))
				{
					$item[$i]['id'] = $row['id'];
					$item[$i]['parent_id'] = $row['parent_id'];
					$item[$i]['title'] = $row['title'];
					$i++;
				}
			}
			return $item;
		}
	}
	
	function getProductMenuArray($site)
	{
		if($site > 0)
		{
			$i = 0;
			$result = mysql_query('SELECT * FROM '.DB_PERFIX.self::getDataTablePrefix().self::getDataTable().' WHERE site_id='.$site.' AND link_type=6 ORDER BY menu_order ASC;');
			while($row = mysql_fetch_array($result))
			{ 
				$item[$row['title']][$i]['id'] = $row['id'];
				$item[$row['title']][$i]['parent_id'] = $row['parent_id'];
				$i++;
			}
			return $item;
		}
	}
	
	function getSiteComponents($site_id=SITE_ID, $set_id=0)
	{
		$i=0;
		if($set_id > 0)
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'components WHERE (site_id='.$site_id.') AND set_id='.$set_id.' ORDER BY com_id ASC;'); }
		else
		{ $result = mysql_query('SELECT * FROM '.DB_PERFIX.'components WHERE (site_id='.$site_id.') ORDER BY com_id ASC;'); }
		while($row = mysql_fetch_array($result))
		{
			$items[$row['id']]['id'] = $row['id'];
			$items[$row['id']]['set_id'] = $row['set_id'];
			$items[$row['id']]['site_id'] = $row['site_id'];
			$items[$row['id']]['com_id'] = $row['com_id'];
			$items[$row['id']]['name_l'] = $row['name_l'];
			$items[$row['id']]['name_r'] = $row['name_r'];
			$items[$row['id']]['icon'] = $row['icon'];
			$items[$row['id']]['path'] = $row['path'];
			$items[$row['id']]['hfb_set'] = $row['hfb_set'];
			$items[$row['id']]['site_template'] = $row['site_template'];
			$items[$row['id']]['active'] = $row['active'];
			$i++;
		}
		return $items;
	}
}


?>